<template>
  <div v-if="!isDetail" class="shop full-width temporary bg-white content overflow-hidden-y">
    <div class="shop-name">
      应用商店
    </div>
    <div class="flex scrollbar scroll-y items-start row" style="padding: 0 10px;margin: 20px 0;max-height: calc(100% - 101px)">
      <div class="shop-item margin-t-20" style="padding: 0 10px" v-for="(item, index) in menuData" :key="index">
        <div class="shop-wrapper">
          <div class="used" v-if="item.privilegeId !== undefined && openedId.indexOf(item.privilegeId) > -1">已使用</div>
          <div class="icon" :style="{'background': item.bgColor }">
            <q-icon v-if="item.isSvg" class="font" :name="item.icon" />
            <q-icon v-else class="font" :name="`iconfont ${item.icon}`" :style="{'color': item.iconColor }"></q-icon>
          </div>
          <div class="content-wrapper">
            <div>
              <div class="title">{{item.title}}</div>
              <div class="content">
                {{ item.desc }}
              </div>
            </div>
            <div v-if="item.privilegeId === undefined || openedId.indexOf(item.privilegeId) === -1" class="more" style="color: #5F77FD;" @click="goDetail(item)">
              了解更多 >
            </div>
            <div v-else class="more" @click="goDetail(item)">
              了解更多 >
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div v-else class="shop full-width temporary bg-white flex padding-t-20 scrollbar items-start content">
    <detail :info="info"  @back="isDetail=false"/>
  </div>
</template>

<script>
import { menuData } from './data'
import { mapGetters } from 'vuex'
import { getServicePeriod } from '../../../boot/home/user'
import detail from './detail'

export default {
  name: 'shop',
  components: {
    detail
  },
  data () {
    return {
      menuData,
      openedId: [],
      isDetail: false,
      info: {}
    }
  },
  computed: {
    ...mapGetters([
      'userInfo'
    ])
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      // 获取所开通的模块
      const userId = this.userInfo.user_id
      getServicePeriod(userId).then(res => {
        const response = res.data
        const result = [-1, 0]
        response.forEach(item => {
          // 如果存在开始时间和结束时间的话，判断当前时间是否在开始时间和结束时间内
          // 否则就是无限期，直接push
          if (item.endDate && item.startDate) {
            const now = new Date().getTime()
            if (now > new Date(item.startDate).getTime() && now < new Date(item.endDate).getTime()) {
              result.push(item.privilegeId)
            }
          } else {
            result.push(item.privilegeId)
          }
        })
        this.openedId = result
      })
    },
    goDetail (info) {
      this.isDetail = true
      this.info = info
    }
  }
}
</script>

<style scoped lang="scss">
.shop-wrapper {
  height: 206px;
  background: #FFFFFF;
  border: 1px solid #D3D3D3;
  border-radius: 6px;
  position: relative;
  overflow: hidden;

  .used{
    width: 80px;
    height: 30px;
    background: #5F77FD;
    border-radius: 0px 6px 0px 30px;
    position: absolute;
    right: 0;
    top: 0;
    text-align: center;
    color: #ffffff;
    line-height: 30px;
  }

  .icon{
    width: 48px;
    height: 48px;
    background: rgba(205, 213, 255, 0.3);
    border-radius: 20px;
    text-align: center;
    line-height: 48px;
    position: absolute;
    left: 20px;
    top: 20px;

    .font{
      font-size: 20px;
      color: rgba(95, 119, 253, 1);
    }
  }

  .content-wrapper{
    position: absolute;
    left: 83px;
    padding-top: 30px;
    padding-right: 30px;
    padding-bottom: 30px;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .title {
      font-size: 18px;
      font-weight: bold;
      color: #333333;
    }

    .content {
      font-weight: 400;
      color: #666666;
      padding: 10px 0 20px 0;
    }

    .more {
      font-weight: 500;
      color: #999999;
      cursor: pointer;
    }
  }
}

.shop-wrapper:hover {
  border-color: #5F77FD;
}
.shop-item:nth-child(-n+3){
  margin-top: 0;
}
@media (min-width: 1800px){
  .shop-item {
    width: 25%;
  }
  .shop-item:nth-child(-n+4){
    margin-top: 0;
  }
}
@media (max-width: 1799px) {
  .shop-item {
    width: calc(100% / 3);
  }
  .shop-item:nth-child(-n+3) {
    margin-top: 0;
  }
}

.shop-name{
  font-size: 20px;
  font-weight: 500;
  color: #333333;
  padding: 20px;
  line-height: 1;
  border-bottom: 1px solid #d3d3d3;
  background: #ffffff;
}
</style>
